package com.jintian.smart.kernel.security.config;

import com.jintian.smart.kernel.core.constant.StringConst;
import com.jintian.smart.kernel.security.repeatsubmit.RepeatSubmitAspect;
import com.jintian.smart.kernel.security.repeatsubmit.ticket.RepeatSubmitRedisTicket;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import com.jintian.smart.kernel.common.context.ContextHolder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 重复提交配置
 */
@Configuration
public class RepeatSubmitConfig {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Bean
    public RepeatSubmitAspect repeatSubmitAspect() {
        RepeatSubmitRedisTicket ticket = new RepeatSubmitRedisTicket(redisTemplate, this::ticket);
        return new RepeatSubmitAspect(ticket);
    }

    /**
     * 获取指明某个用户的凭证
     */
    private String ticket(HttpServletRequest request) {
        Long userId = ContextHolder.getUserIdLong();
        if (null == userId) {
            return StringConst.EMPTY;
        }
        return request.getServletPath() + "_" + userId;
    }
}
